package mysgoog.util;

import java.io.*;
/**
* @author Fu, Zhongyang
*/
public class PatternGeneration
{

    public static int boardSize = 64;
    FileOutputStream fos = null;
    BufferedWriter bw = null;
    String encoding = "utf-8";
    int count = 0;

    
    public static void main(String arg[]) throws FileNotFoundException,
            UnsupportedEncodingException
    {
        new PatternGeneration("pattern\\takamoku");
    }

    public PatternGeneration(String filename) throws FileNotFoundException,
            UnsupportedEncodingException
    {
        File file = new File("pattern\\takamoku.pattern");
        fos = new FileOutputStream(file);
        bw = new BufferedWriter(new OutputStreamWriter(fos, encoding));
        read(filename);
    }

    public void write(String text) throws IOException
    {
        bw.write(text);
    }

    public void read(String filename)
    {
        BufferedReader reader = null;
        try
        {
            File file = new File(filename);
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            while ((text = reader.readLine()) != null)
            {
                generate(text);

            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    // O stand for player X stand for opponent
    public void generate(String text) throws IOException
    {
        String move[] = text.split(" ");
        char[] board = new char[boardSize];
        for (int i = 0; i < boardSize; i++)
            board[i] = '.';
        // System.out.println(text + " " + move.length);
        char current = move[move.length - 1].charAt(0);
        for (int i = 0; i < move.length - 1; i++)
        {
            int m = parseMove(move[i]);
            if (m < 0)
                return;
            if (move[i].charAt(0) == current)
                board[m] = 'O';
            if (move[i].charAt(0) != current)
                board[m] = 'X';            
        }
        int lastMove = parseMove(move[move.length - 1]);
        if (lastMove >= 0)
            write((move.length - 1) + " " + parseBoard(board) + " " + lastMove + "\n");

        board = new char[boardSize];
        for (int i = 0; i < boardSize; i++)
            board[i] = '.';
        for (int i = 0; i < move.length - 1; i++)
        {
            int m = parseMoveSym(move[i]);
            if (m < 0)
                return;
            if (move[i].charAt(0) == current)
                board[m] = 'O';
            if (move[i].charAt(0) != current)
                board[m] = 'X';
        }
        lastMove = parseMoveSym(move[move.length - 1]);
        if (lastMove >= 0)
            write((move.length - 1) + " " + parseBoard(board) + " " + lastMove + "\n");
        System.err.println(count++);
    }

    public int parseMove(String move)
    {
        if ((move.charAt(1) > 's') || (move.charAt(1) < 'l'))
            return -1;
        if ((move.charAt(2) > 'h') || (move.charAt(2) < 'a'))
            return -1;
        int y = (move.charAt(1) - 'l');
        int x = (move.charAt(2) - 'a');
        int result = x * 8 + y;
        // System.out.println(move + " " + result);
        return result;
    }

    public int parseMoveSym(String move)
    {
        if ((move.charAt(1) > 's') || (move.charAt(1) < 'l'))
            return -1;
        if ((move.charAt(2) > 'h') || (move.charAt(2) < 'a'))
            return -1;
        int y = (move.charAt(1) - 'l');
        int x = (move.charAt(2) - 'a');
        int newx = (7 - y);
        int newy = (7 - x);
        int result = newx * 8 + newy;
        // System.out.println(move + " " + result);
        return result;
    }

    public String parseBoard(char[] board)
    {
        String result = "";
        for (int i = 0; i < boardSize; i++)
        {
            // if (i % 10 == 0)
            // result +="\n";
            result += board[i];
        }
        return result;
    }
}
